home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0793 / EXECUTE.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-01  |  5KB  |  151 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 152 of 158                                                               
  3. From : Mark Lewis                          1:3634/12.0          18 Jul 93  11:40 
  4. To   : Brian Swanson                       1:123/419.0                           
  5. Subj : Miscellaneous Unit St                                                  
  6. ────────────────────────────────────────────────────────────────────────────────
  7.  >>  way to call exec(); without the output from the
  8.  >> "shell" going to the
  9.  >>  screen?..thanx for any help...
  10.  > I asked about this a few months ago...A few people came up with
  11.  > some jury rigged fixes, but I never found one that really worked
  12.  > well...What you need to do is hook whatever interrupt is called
  13.  > when characters are displayed, and just don't display them...
  14.  
  15. =or= use true redirection... i don't remember who the original author was or
  16. who posted this way back... but here it is, once again... enjoy -=B-)
  17.  
  18. ______________________________O/______ snip _________________________
  19.                               O\}
  20.  
  21. {
  22. Use it exactly as you would the normal EXEC procedure:
  23.  
  24.   Exec('MASM.EXE','mystuff.asm');
  25.  
  26. To activate redirection simply add the redirection symbols, etc:
  27.  
  28.   Exec('MASM.EXE','mystuff.asm >err.lst');
  29.  
  30. One note of caution.  This routine temporarily uses extra handles. It's
  31. either two or four more.  The various books I have are not clear as to
  32. whether duplicated handles 'count' or not. My guess is yes.  If you don't
  33. plan on redirecting STDIN then remove all the code for duplicating it to
  34. cut your handle overhead in half.
  35. }
  36.  
  37. Unit Execute;
  38.  
  39. Interface
  40.  
  41. Procedure Exec(Path,CmdLine : String);
  42.  
  43. Implementation
  44.  
  45. Uses Dos;
  46.  
  47. Function ExtractFileName(Var Line : String;Index : Integer) : String;
  48.  
  49. Var
  50.   Temp : String;
  51.  
  52. Begin
  53.   Delete(Line,Index,1);
  54.   While (Index <= Length(Line)) AND (Line[Index] = ' ')
  55.     Do Delete(Line,Index,1);
  56.   Temp := '';
  57.   While (Index <= Length(Line)) AND (Line[Index] <> ' ') Do
  58.   Begin
  59.     Temp := Temp + Line[Index];
  60.     Delete(Line,Index,1);
  61.   End;
  62.   ExtractFileName := Temp;
  63. End;
  64.  
  65. Procedure CloseHandle(Handle : Word);
  66.  
  67. Var
  68.   Regs : Registers;
  69.  
  70. Begin
  71.   With Regs Do
  72.   Begin
  73.     AH := $3E;
  74.     BX := Handle;
  75.     MsDos(Regs);
  76.   End;
  77. End;
  78.  
  79. Procedure Duplicate(SourceHandle : Word;Var TargetHandle : Word);
  80.  
  81. Var
  82.   Regs : Registers;
  83.  
  84. Begin
  85.   With Regs Do
  86.   Begin
  87.     AH := $45;
  88.     BX := SourceHandle;
  89.     MsDos(Regs);
  90.     TargetHandle := AX;
  91.   End;
  92. End;
  93.  
  94. Procedure ForceDuplicate(SourceHandle : Word;Var TargetHandle : Word);
  95.  
  96. Var
  97.   Regs : Registers;
  98.  
  99. Begin
  100.   With Regs Do
  101.   Begin
  102.     AH := $46;
  103.     BX := SourceHandle;
  104.     CX := TargetHandle;
  105.     MsDos(Regs);
  106.     TargetHandle := AX;
  107.   End;
  108. End;
  109.  
  110. Procedure Exec(Path,CmdLine : String);
  111.  
  112. Var
  113.   StdIn   : Word;
  114.   Stdout  : Word;
  115.   Index   : Integer;
  116.   FName   : String[80];
  117.   InFile  : Text;
  118.   OutFile : Text;
  119.  
  120.   InHandle  : Word;
  121.   OutHandle : Word;
  122.          { ===============>>>> }   { change below for STDERR }
  123. Begin
  124.   StdIn := 0;
  125.   StdOut := 1;                    { change to 2 for StdErr       }
  126.   Duplicate(StdIn,InHandle);      { duplicate standard input     }
  127.   Duplicate(StdOut,OutHandle);    { duplicate standard output    }
  128.   Index := Pos('>',CmdLine);
  129.   If Index > 0 Then               { check for output redirection }
  130.   Begin
  131.     FName := ExtractFileName(CmdLine,Index);  { get output file name  }
  132.     Assign(OutFile,FName);                    { open a text file      }
  133.     Rewrite(OutFile);                         { .. for output         }
  134.     ForceDuplicate(TextRec(OutFile).Handle,StdOut);{ make output same }
  135.   End;
  136.   Index := Pos('<',CmdLine);
  137.   If Index > 0 Then               { check for input redirection }
  138.   Begin
  139.     FName := ExtractFileName(CmdLine,Index);  { get input file name  }
  140.     Assign(InFile,FName);                     { open a text file     }
  141.     Reset(InFile);                            { for input            }
  142.     ForceDuplicate(TextRec(InFile).Handle,StdIn);  { make input same }
  143.   End;
  144.   DOS.Exec(Path,CmdLine);           { run EXEC }
  145.   ForceDuplicate(InHandle,StdIn);   { put standard input back to keyboard }
  146.   ForceDuplicate(OutHandle,StdOut); { put standard output back to screen  }
  147.   CloseHandle(InHandle);            { close the redirected input file     }
  148.   CloseHandle(OutHandle);           { close the redirected output file    }
  149. End;
  150.  
  151. End.